﻿using System.Windows;
using System.Windows.Controls;
using SHomeWorkshop.LunarPresentation.Interfaces;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 切换[水平/垂直]对齐方式。
        /// </summary>
        public static void SwitchDirection()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is IDirection)
            {
                if (curEx is Expressions.SubPanel)
                {
                    foreach (UIElement ue in (curEx as Expressions.SubPanel).BasePanel.Children)
                    {
                        Expressions.BaseText bt = ue as Expressions.BaseText;

                        if (bt != null && bt.IsCollapsed)
                        {
                            MessageBox.Show("此子面板中有标题文本块处于“折叠”状态，不允许切换为水平指向！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                    }
                }

                ModifingItem mi = new ModifingItem();
                Action action;

                IDirection idrection = curEx as IDirection;
                if (idrection.Direction == Orientation.Horizontal)
                {
                    action = new Action(curEx.ExID, -1, curEx.GetType().Name, "Direction",
                        idrection.Direction.ToString(), Orientation.Vertical.ToString());
                    idrection.Direction = Orientation.Vertical;
                }
                else
                {
                    action = new Action(curEx.ExID, -1, curEx.GetType().Name, "Direction",
                           idrection.Direction.ToString(), Orientation.Horizontal.ToString());
                    idrection.Direction = Orientation.Horizontal;
                }

                Globals.MainWindow.btnIndent.IsEnabled = Command.CanIndent(curEx);
                Globals.MainWindow.btnOutdent.IsEnabled = Command.CanOutdent(curEx);

                mi.Add(action);
                curEx.Editor.Do(mi);

                curEx.Editor.IsModified = true;
            }
            else
            {
                // 不支持IDirection接口，肯定不是子面板。
                if (curEx.ParentPanel != null)
                {
                    Expressions.SubPanel parentPanel = curEx.ParentPanel;

                    foreach (UIElement ue in parentPanel.BasePanel.Children)
                    {
                        Expressions.BaseText bt = ue as Expressions.BaseText;
                        if (bt != null)
                        {
                            if (bt.IsCollapsed)
                            {
                                MessageBox.Show("当前公式父面板中有标题文本块处于“折叠”状态，不允许切换为水平指向！",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }
                        }
                    }

                    ModifingItem mi = new ModifingItem();
                    Action action;

                    if (parentPanel.Direction == Orientation.Horizontal)
                    {
                        action = new Action(parentPanel.ExID, -1, parentPanel.GetType().Name, "Direction",
                              parentPanel.Direction.ToString(), Orientation.Vertical.ToString());
                        parentPanel.Direction = Orientation.Vertical;
                    }
                    else
                    {
                        action = new Action(parentPanel.ExID, -1, parentPanel.GetType().Name, "Direction",
                              parentPanel.Direction.ToString(), Orientation.Horizontal.ToString());
                        parentPanel.Direction = Orientation.Horizontal;
                    }

                    Globals.MainWindow.btnIndent.IsEnabled = Command.CanIndent(curEx);
                    Globals.MainWindow.btnOutdent.IsEnabled = Command.CanOutdent(curEx);

                    mi.Add(action);
                    parentPanel.Editor.Do(mi);

                    return;
                }

                // 其实应该不会有机会弹出这个消息框。
                MessageBox.Show("当前选中的公式不支持【水平∕垂直】指向的切换操作！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
        }
    }
}
